import vue from 'vue'
import axios from 'axios'
import router from '../../router'
import { Message, Loading } from 'element-ui';
vue.component(Message.name, Message);
vue.use(Loading);
// http request 拦截器
axios.interceptors.request.use(
  config => {
    var isPro = process.env.NODE_ENV === 'production'//production == 生产，development ==开发
    if (isPro) {
      config.url = url + config.url
    }
    if (localStorage.getItem('authorization')) {  // 判断是否存在token，如果存在的话，则每个http header都加上token
      config.headers.Authorization = localStorage.getItem('authorization');
    }
    return config;
  },
  err => {
    return Promise.reject(err);
  }
);
//响应拦截器
axios.interceptors.response.use(function (response) {
  if (response && response.data && response.data.statusCode == 401) {
    return window.vm.$router.push({ path: '/login' })
  }
  // 在接收响应做些什么，例如跳转到登录页
  if (response) {
    let authorization = response.headers.authorization;

    if (authorization != '' && authorization != undefined) {
      localStorage.setItem('authorization', authorization);
    }
  }
  return response;
}, function (error) {
  // 对响应错误做点什么
  if (error.response.status == 401) {
    this.$router.push({ path: '/login' })
    localStorage.removeItem('authorization');
    localStorage.removeItem('loginToken');
  }
  return Promise.reject(error);
}
);


export function get(url, params = {}, load) {
  return new Promise((resolve /*,reject*/) => {
    if (url.indexOf("?") > -1) {
      url += "&dataTime=" + new Date().getTime();
    } else {
      url += "?dataTime=" + new Date().getTime();
    }
    let loading = '';
    if (load) {
      loading = this.$loading({
        lock: true,
        text: '请稍等',
        customClass: 'jiazaizhong',
        spinner: 'el-icon-loading',
        background: 'rgba(255,255,255, 0.7)'
      });
    }
    axios.get(url, {
      params: params
    })
      .then(response => {
        if (response.data.statusCode == 200) {
          resolve(response);
        } else {
          this.$message({
            message: response.data.msg,
            type: 'warning'
          });
        }
        if (loading)
          loading.close();
      })
      .catch(err => {
        //reject(err)
        if (err && err.response && err.response.data && err.response.data.msg) {
          this.$message({
            message: err.response.data.msg,
            type: 'warning'
          });
        } else {
          this.$message({
            message: '请求超时',
            type: 'warning'
          });
        }
        if (loading)
          loading.close();
      })
  })
}
export function post(url, data = {}) {
  return new Promise((resolve, reject) => {
    let _this = this;
    if (url.indexOf("?") > -1) {
      url += "&dataTime=" + new Date().getTime();
    } else {
      url += "?dataTime=" + new Date().getTime();
    }
    const loading = this.$loading({
      lock: true,
      text: '请稍等',
      customClass: 'jiazaizhong',
      spinner: 'el-icon-loading',
      background: 'rgba(255,255,255, 0.7)'
    });
    axios.post(url, data)
      .then(response => {
        if (response.data.statusCode == 200) {
          resolve(response);
        } else {
          _this.$message({
            message: response.data.msg,
            type: 'warning'
          });
        }
        loading.close();
      }, err => {
        if (err && err.response && err.response.data && err.response.data.msg) {
          this.$message({
            message: err.response.data.msg,
            type: 'warning'
          });
        } else {
          this.$message({
            message: '请求超时',
            type: 'warning'
          });
        }
        loading.close();
      })
  })
}
export function delet(url, data = {}) {
  return new Promise((resolve, reject) => {
    if (url.indexOf("?") > -1) {
      url += "&dataTime=" + new Date().getTime();
    } else {
      url += "?dataTime=" + new Date().getTime();
    }
    const loading = this.$loading({
      lock: true,
      text: '请稍等',
      customClass: 'jiazaizhong',
      spinner: 'el-icon-loading',
      background: 'rgba(255,255,255, 0.7)'
    });
    axios.delete(url, data)
      .then(response => {
        if (response.data.statusCode == 200) {
          resolve(response);
        } else {
          this.$message({
            message: response.data.msg,
            type: 'warning'
          });
        }
        loading.close();
      }, err => {
        if (err && err.response && err.response.data && err.response.data.msg) {
          this.$message({
            message: err.response.data.msg,
            type: 'warning'
          });
        } else {
          this.$message({
            message: '请求超时',
            type: 'warning'
          });
        }
        loading.close();
      })
  })
}
export function put(url, data = {}) {
  return new Promise((resolve, reject) => {
    if (url.indexOf("?") > -1) {
      url += "&dataTime=" + new Date().getTime();
    } else {
      url += "?dataTime=" + new Date().getTime();
    }

    const loading = this.$loading({
      lock: true,
      text: '请稍等',
      customClass: 'jiazaizhong',
      spinner: 'el-icon-loading',
      background: 'rgba(255,255,255, 0.7)'
    });
    axios.put(url, data)
      .then(response => {
        if (response.data.statusCode == 200) {
          resolve(response);
        } else {
          this.$message({
            message: response.data.msg,
            type: 'warning'
          });
        }
        loading.close();
      }, err => {
        if (err && err.response && err.response.data && err.response.data.msg) {
          this.$message({
            message: err.response.data.msg,
            type: 'warning'
          });
        } else {
          this.$message({
            message: '请求超时',
            type: 'warning'
          });
        }
        loading.close();
      })
  })
}
